import pymongo
from datetime import datetime, date
import sys
from datetime import timedelta
client = pymongo.MongoClient('mongodb://cs_user:cmcc5188@localhost:12018/cs_recharge')
db = client.cs_recharge
coll = db.rechange
import redis
conn = redis.Redis()
today = date.today()
yesterday = date.today() - timedelta(1)
yesterday = yesterday.strftime('%Y%m%d')
today = today.strftime('%Y%m%d')
test_range = coll.find({ "orderstate": "9", "tel_type": "unicom", "orderdate": today})
for i in test_range:
    # print(i)
    if i.get('query_time', 0) - i['time'] >  420:
        if not conn.sismember('risk_number', i['tel_number']):
            print(i['tel_number'])
            conn.sadd('risk_number', i['tel_number'])
print('空号--------------')
msg1 = "尊敬的用户您好，您输入的号码无法使用该业务，详情请咨询10010。[1118]"
msg2 = "尊敬的用户您好，您的号码未通过联通号码校验，请确认您的号码是否为联通号码。[9008]"
msg3 = "尊敬的用户您好，您输入的交费充值号码不存在，请重新输入。[1111]"
msg4 = "尊敬的用户您好，您输入的号码为智能网号码，网上营业厅暂不支持该类号码网上交费,请使用银行卡充值或一卡充充值，敬请谅解。[2002]"
msg = '尊敬的用户您好，您输入的交费充值号码不存在，请重新输入。[2001]'
msg0 = '尊敬的用户您好，您输入的交费充值号码不存在，请重新输入。[2202]'
test_range = coll.find({"orderdate": today, "tel_type": "unicom", "orderstate": "1", "message": {"$in": [msg, msg0, msg1, msg2, msg3, msg4]}})
for i in test_range:
    # print(i)
    if not conn.sismember('risk_number', i['tel_number']):
        print(i['tel_number'])
        conn.sadd('risk_number', i['tel_number'])
print('繁忙--------------')
msg5 = '尊敬的用户您好，目前系统繁忙，请稍后再试，由此给您造成的不便，敬请谅解'
msg9 = '您的操作太频繁，请稍后再试！'
# msg7 = '尊敬的用户您好，目前系统繁忙，请稍后再试，由此给您造成的不便，敬请谅解。[0121]'
# res = coll.aggregate([{"$match": {"orderstate": "1", "tel_type": "unicom", "message": {"$in": [msg5, msg6, msg8]}, 'orderdate': today}}, {"$group": {"_id": "$tel_number", "count": {"$sum": 1}}}])
res = coll.aggregate([{"$match": {"orderstate": "1", "tel_type": "unicom", "message": {"$regex": "尊敬的用户您好，目前系统繁忙，请稍后再试，由此给您造成的不便，敬请谅解.*"}, 'orderdate': today}}, {"$group": {"_id": "$tel_number", "count": {"$sum": 1}}}])
for i in res:
    if i['count'] > 3:
        if not conn.sismember('risk_number', i['_id']):
            print(i['_id'])
            conn.sadd('risk_number', i['_id'])
print('频繁号---------------')
res = coll.aggregate([{"$match": {"orderstate": "1", "tel_type": "unicom", "message": msg9, 'orderdate': today}}, {"$group": {"_id": "$tel_number", "count": {"$sum": 1}}}])
for i in res:
    if i['count'] >= 2:
        if not conn.sismember('risk_number', i['_id']):
            print(i['_id'])
            conn.sadd('risk_number', i['_id'])
print(conn.scard('risk_number'))

